package com.java.basis.thread.cas.atomic;

import org.apache.commons.lang.builder.ToStringBuilder;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * AtomicReferenceFieldUpdater的使用
 * 使用功能此功能修改属性的时候，必须要给对应的属性加上volatile
 *
 * @author wangning
 * @create 2022-01-29 10:29
 */
public class AtomicReferenceFieldUpdaterTest01 {
	public static void main(String[] args) {
		Student student = new Student();
		AtomicReferenceFieldUpdater<Student, String> updater
				= AtomicReferenceFieldUpdater.newUpdater(Student.class, String.class, "name");
		updater.compareAndSet(student, null, "张三");
		System.out.println(student);

	}
}

class Student {
	public volatile String name;

	@Override
	public String toString() {
		return new ToStringBuilder(this)
				.append("name", name)
				.toString();
	}
}
